/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.database.bean;

import com.database.dao.PerfilDao;
import com.database.dao.impl.PerfilDaoImplement;
import com.database.model.Perfil;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author diego
 */
@FacesConverter(value = "perfilConverter")
public class PerfilConverter implements Converter{

   
    /**
     * Creates a new instance of UsuarioConverter
     */
    public PerfilConverter() {
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String newValue) {
          Perfil perfil = null;
        try {
            PerfilDao perfilDao = new PerfilDaoImplement();
            perfil = perfilDao.buscarPorId(Integer.parseInt(newValue));
        }catch(Throwable ex) {
//            ResourceBundle bundle = ResourceBundle.getBundle("messages");
//            FacesMessage msg = new FacesMessage(bundle.getString("faculty_convertion_message"));
            FacesMessage msg = new FacesMessage("Hubo un problema de Conversion a Objeto");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
 //           throw new ConverterException(msg);
        }
        return perfil;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
         String val = null;
        try {
            Perfil perfil = (Perfil) value;
            val = Integer.toString((perfil.getCodPerfil()));
        }catch(Throwable ex) {
          //  ResourceBundle bundle = ResourceBundle.getBundle("messages");
            FacesMessage msg = new FacesMessage("Hubo un problema de conversion a String");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
//            throw new ConverterException(msg);
        }
        return val;
    }


}

